package is.glif.fenius.webui.topo;


import is.glif.fenius.webui.common.WebUIException;

import org.apache.log4j.Logger;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class SimpleGraphProvider implements GraphProvider {
    private static Logger log = Logger.getLogger(SimpleGraphProvider.class);
    private static SimpleGraphProvider instance;
    public static SimpleGraphProvider getInstance() {
        if (instance == null) {
            instance = new SimpleGraphProvider();
        }
        return instance;
    }
    private SimpleGraphProvider() {
        
    }
    public void refresh() {
        FileEdgeProvider.getInstance().refresh();
    }
    

    public JSONArray getGraph() throws WebUIException {
        String[] colors = {
                "#3465a4",
                "#73d216",
                "#f57900",
                "#edd400",
                "#c17d11",
                "#75507b",
                "#cc0000",
                "#555753",
        };
        
        JSONArray graph = new JSONArray();
        
        FileEdgeProvider ep = FileEdgeProvider.getInstance();
        FileNetworkProvider np = FileNetworkProvider.getInstance();
        try {
            JSONArray networks = np.getNetworks("");
            for (int i = 0; i < networks.length(); i++) {
                JSONObject networkObj = networks.getJSONObject(i);
                String netId = networkObj.getString("id");
                String netName = networkObj.getString("name");
                
                JSONObject nodeData = new JSONObject();
                nodeData.put("$type", "circle");
                nodeData.put("$dim", "5");
                int colorIdx = i % colors.length;
                nodeData.put("$color", colors[colorIdx]);
                
                
                JSONObject graphNode = new JSONObject();
                graphNode.put("id", netId);
                graphNode.put("name", netName);
                graphNode.put("data", nodeData);
                JSONArray graphAdjs = new JSONArray();
                graphNode.put("adjacencies", graphAdjs);
                
                
                boolean gotOne = false;
                JSONArray edges = ep.getEdges(netId, "", "");
                for (int j = 0; j < edges.length(); j++) {
                    JSONObject edgeObj = edges.getJSONObject(j);
                    String urn = (String) edgeObj.get("urn");
                    if (edgeObj.has("adjacencies")) {
                        JSONArray adjacencies = edgeObj.getJSONArray("adjacencies");
                        for (int k = 0; k < adjacencies.length(); k++) {
                            JSONObject adjData = adjacencies.getJSONObject(k);
                            String adjType = adjData.getString("type");
                            if (adjType.equals("network")) {
                                String adjNet  = adjData.getString("peer");
                                JSONObject graphAdj = new JSONObject();
                                JSONObject graphAdjData = new JSONObject();
                                graphAdj.put("nodeTo", adjNet);
                                graphAdj.put("data", graphAdjData);
                                graphAdjData.put("$color", "#557eaa");
                                graphAdjs.put(graphAdj);
                                gotOne = true;
                            }
                        }
                    } else {
                        log.debug("no adjacencies for "+ urn);
                    }
                }
                if (gotOne) {
                    graph.put(graphNode);
                }
            }
        } catch (JSONException e) {
            log.error(e);
            throw new WebUIException("Internal error creating graph");
        }

        return graph;
    }

}
